﻿function EnableUnRead() {
    // Get the client context first
    var ctx = SP.ClientContext.get_current();
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);

    if (items.length == 0) {
        return false;
    }

    // Check whether all the selected items are read, otherwise disable the unread button
    for (i = 0; i < items.length ; i++) {
        var currentItem = items[i];
        for (j = 0; j < listData.Row.length; j++) {
            var currentRow = listData.Row[j];
            if (currentRow.ID == currentItem.id) {
                if (currentRow.Readed != "1") {
                    return false;
                }
            }
        }
    }

    return true;
}

function EnableUnReadContextMenu() {

    // To do: Add your logical for how to determine enable or disable the unread context menu for current item
    // Seems the item context menu don't support EnabledScript, need further investigation here
    return false;
}

function ClickUnReadRibbonButton() {

    // To do: Add your logical for operating the items once the end use click UnReadRibbonButton
    var selectedRows = GetSelectedDataRows();
    var ctx = SP.ClientContext.get_current();
    var readList = ctx.get_web().get_lists().getByTitle("ReadList");
    for (i = 0; i < selectedRows.length; i++) {
        var currentRow = selectedRows[i];
        var deleteItem = readList.getItemById(currentRow.readItemId);
        deleteItem.deleteObject();

        currentRow.Readed = currentRow.readMarkId;
        var readMarkPlaceHolder = document.getElementById(currentRow.Readed);
        BoldItemRow(readMarkPlaceHolder);
    }

    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onDeleteSucceeded),
        Function.createDelegate(this, this.onDeleteFailed)
    );

   
    // alert("ClickUnReadRibbonButton");
}

function ClickUnReadContextMenu() {    
    // Check whether the selected item is read, it not dispaly a message says that message is already unread status
    var ctx = SP.ClientContext.get_current();
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var currentRow;
    for (j = 0; j < listData.Row.length; j++) {
         currentRow = listData.Row[j];
        if (currentRow.ID == items[0].id) {
            if (currentRow.Readed != "1") {
                alert("This item already unread");
                return;
            }

            break;
        }
    }

    // To do: Add your logical for operating the items once the end use click UnReadContextMenu
    // Delete the remarkId from read list currentRow.readItemId
    var readList = ctx.get_web().get_lists().getByTitle("ReadList");
    var deleteItem = readList.getItemById(currentRow.readItemId);
    deleteItem.deleteObject();
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onDeleteSucceeded),
        Function.createDelegate(this, this.onDeleteFailed)
    );

    // Update the readed property to readMarkId
    currentRow.Readed = currentRow.readMarkId;

    // Update the item row style to bold
    var readMarkPlaceHolder = document.getElementById(currentRow.Readed);
    BoldItemRow(readMarkPlaceHolder);
}

// Bold the item row
function BoldItemRow(readMarkPlaceHolder) {

    readMarkPlaceHolder.innerText = "Unread";
    var parentTR =  getParentNode_TR(readMarkPlaceHolder.parentNode);
    parentTR.style.fontWeight = 'bold';
}

// Handle the on delete successed event
function onDeleteSucceeded() {
}

// Handle the on delete failed event
function onDeleteFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +  '\n' + args.get_stackTrace());

}

// Get the selected data rows
function GetSelectedDataRows() {
    var ctx = SP.ClientContext.get_current();
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var selectdRows = new Array(items.length);    
    for (i = 0; i < items.length ; i++) {
        var currentItem = items[i];
        for (j = 0; j < listData.Row.length; j++) {
            var currentRow = listData.Row[j];
            if (currentRow.ID == currentItem.id) {
                selectdRows[i] = currentRow;
            }
        }
    }

    return selectdRows;
}